<?php
/**
 * 订单模型
 * 
 * @wandaijin
 */

namespace Common\Model;
use Think\Model;

class XsOrderModel extends Model{

    protected $tableName = 'xs_order';
    
    /**
     * 保存数据到数据库
     */
    public function saveOrder($data)
    {
        $data_save = $data;
        $data_save['net_order_sn'] = $this->makeOrderSN();
        $data_save['date'] = date("Y-m-d", NOW_TIME);
        $data_save['create_time'] = NOW_TIME;
        
        $id = $this->add($data_save);
        if ($id) {
            $data_save['id'] = $id;
            return $data_save;
        } else {
            return false;
        }
    }
    
    /**
     * 删除订单
     * @param unknown $order_sn
     */
    public function deleteOrder($net_order_sn)
    {
        return $this->where(array("net_order_sn"=>$net_order_sn))->delete();
    }
    
    /**
     * 获取订单详情
     * @param unknown $order_id
     */
    public function getDetail($order_id)
    {
        return $this->where(array("id"=>$order_id))->find();
    }
    
    /**
     * 获取订单详情
     * @param unknown $order_id
     */
    public function getDetailByOrderSN($order_sn)
    {
        return $this->where(array("net_order_sn"=>$order_sn))->find();
    }
    
    /**
     * 生成订单编号
     * @return string
     * author CtrL
     */
    public function makeOrderSN(){
        $timestr = str_replace('.','', strval(microtime(true)) );
        $orderSN = date('ymdH').rand(1,9). substr($timestr, 9,3);
        return $orderSN;
    }
    
    /**
     * 向小帅云端发起支付请求
     */
    public function callPayHandle($order_id)
    {
        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "handlePayRequest";
            //需要传递的参数
            $params['order_id'] = $order_id;
            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");
            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }
                
                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {
                return $res['data'];
            }
        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }
    }
    
    /**
     * 向小帅云端发起退款请求
     * @param unknown $host_name
     * @param unknown $cinema_order_sn
     */
    public function callRefundHandle($host_name, $cinema_order_sn)
    {
        //只能向支付成功的订单申请退款
        $pay_status =  $this->where(array('cinema_order_sn'=>$cinema_order_sn, 'host_name'=>$host_name))
        ->getField('pay_status');
        
        //如果因为网络原因，用户端没有成功，服务端已经成功，则直接返回退款成功
        if ($pay_status == 2) {
            return array('error_code'=>0, 'message'=>'订单已经退款成功');
        } else if ($pay_status != 1) {
            return array('error_code'=>1, 'message'=>'订单未成功支付，无法办理退款');
        }
        
        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "handleRefundRequest";
            //需要传递的参数
            $params['host_name'] = $host_name;
            $params['cinema_order_sn'] = $cinema_order_sn;
        
            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");
            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }
                
                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {
                return $res['data'];
            }
        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }
    }
    
    /**
     * 设置发票信息
     * @param unknown $order_sn
     * @param unknown $value
     */
    public function setInvoice($host_name, $cinema_order_sn, $value)
    {
        return $this->where(array('cinema_order_sn'=>$cinema_order_sn, 'host_name'=>$host_name))
        ->setField('invoice', $value);
    }
}
